/*
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company: CoE
 * @author: Mr.Nattasan Wang-Aphisit
 * @version 1.1
 */

package wads.app;

import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextField;
import wads.com.WADTools;
import wads.drawing.node.DocumentNode;

public class DocumentNodeDialog extends BaseWADDialog {
//  private static final long serialVersionUID = -5140204883414789367L;

  private JLabel templateLabel = new JLabel("Template Name");
  private JTextField docField = new JTextField();
  private JLabel sourceLabel = new JLabel("HTML Source");
  private JTextField pathField = new JTextField();
  private JButton browseButton = new JButton("Browse..");
  private JFileChooser chooser = new JFileChooser();
  
  private static final int DOC_DIALOG_HEIGHT = 270;

  private DocumentNode document;
  private File currentDirectory;

  public DocumentNodeDialog(Frame parent, DocumentNode document) {
    super(parent, document);
    this.setTitle("Get HTML Form");
    this.document = document;

    try {
      jbInit();
    }
    catch(Exception e) {
        e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.getContentPane().setLayout(null);
    templateLabel.setBounds(new Rectangle(10, 15, 100, 25));
//    templateLabel.setFont(labelFont);
    docField.setBounds(new Rectangle(110, 15, 180, 25));
//    docField.setFont(labelFont);
//    sourceLabel.setFont(labelFont);
    sourceLabel.setBounds(new Rectangle(10, 65, 80, 25));
//    pathField.setFont(labelFont);
    pathField.setBounds(new Rectangle(110, 65, 180, 25));
    if(getDocument().getPrototypePath() != null) {
        pathField.setText(getDocument().getPrototypePath());
    }

    browseButton.setBounds(new Rectangle(295, 65, 80, 25));
    browseButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        browseButton_actionPerformed(e);
      }
    });
//    browseButton.setFont(labelFont);

    this.getContentPane().add(templateLabel, null);
    this.getContentPane().add(docField, null);
    this.getContentPane().add(sourceLabel, null);
    this.getContentPane().add(pathField, null);
    this.getContentPane().add(browseButton, null);
    this.setSize(400, DOC_DIALOG_HEIGHT);
    setOptionButtonsPanelLocation(DOC_DIALOG_HEIGHT);
  }

  private void browseButton_actionPerformed(ActionEvent e) {
    String[] ext = {"html", "htm"};
    chooser = new JFileChooser(WADTools.FORM_PATH);
    chooser.setMultiSelectionEnabled(false);
    chooser.setFileFilter(new HTMLFilter(ext, "HTML Text Form."));
//    if(pathField.getText() != null || !(pathField.getText().equals(""))) {
//        chooser.setCurrentDirectory(new File(pathField.getText()).getParentFile());
//    }
    if(document.getPrototypePath() != null) {
        chooser.setCurrentDirectory(new File(document.getPrototypePath()).getParentFile());
    }

    //check return value of JFileChooser to protect NullPointerException
    int returnVal = chooser.showDialog(this, "Select");
    if(returnVal == JFileChooser.APPROVE_OPTION){
      System.out.println(chooser.getSelectedFile().toString());
      pathField.setText(chooser.getSelectedFile().toString());
    }
    else if(returnVal == JFileChooser.CANCEL_OPTION){
      return;
    }
    else
      return;
  }

  @Override
  public void saveProperties(){
    //check field of user
    if(docField.getText() != null && pathField.getText() != null){
      getOwnerNode().setName(docField.getText());
      ((DocumentNode)getOwnerNode()).setPrototypePath(pathField.getText());
    }
  }

  @Override
  public void initDisplay(){
      if(!getDocument().getName().equals("[ Unregisted Document ]")) {
        docField.setText(((DocumentNode)this.getOwnerNode()).getName());
      }
      else {
          docField.setText("");
      }
      pathField.setText(((DocumentNode)this.getOwnerNode()).getPrototypePath());
  }

    /**
     * @return the document
     */
    public DocumentNode getDocument() {
        return document;
    }

    /**
     * @param document the document to set
     */
    public void setDocument(DocumentNode document) {
        this.document = document;
    }

    /**
     * @return the currentDirectory
     */
    public File getCurrentDirectory() {
        return currentDirectory;
    }

    /**
     * @param currentDirectory the currentDirectory to set
     */
    public void setCurrentDirectory(File currentDirectory) {
        this.currentDirectory = currentDirectory;
        chooser.setCurrentDirectory(currentDirectory);
    }

}